const CracoLessPlugin = require('craco-less')
const path = require('path')
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
// git插件实例
const gitReversionPlugin = new GitRevisionPlugin({
  versionCommand: 'describe --always --tags --dirty'
})
// 以当前文件所处的地址为起点来查找某一个目录的绝对的地址
const pathResolve = pathUrl => path.join(__dirname, pathUrl)
module.exports = {
  webpack: {
    alias: {
      '@': pathResolve('src'),
      // '@styles': pathResolve('src/styles'),
    }
  },
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: {
              '@primary-color': 'rgba(255, 0, 51, 1)'
            },
            javascriptEnabled: true,
          },
        },
      },
    },
    {
      // 自定义的操作
      plugin: {
        // 这里可以直接拿到webpack的配置, 进行修改
        overrideWebpackConfig: ({ webpackConfig, cracoConfig, pluginOptions, context: { env, paths } }) => {
          // console.log(webpackConfig.plugins);
          // 进行打包地址修改
          if (env === 'production') {
            paths.appBuild = path.join(path.dirname(paths.appBuild), 'dist')
            webpackConfig.output.path = path.join(path.dirname(webpackConfig.output.path), 'dist')
            //
            webpackConfig.output.publicPath = './'
          }
          // 全局环境变量
          const definePluginIndex = env === 'development' ? 3 : 4
          const definePlugin = webpackConfig.plugins[definePluginIndex]
          const definitions = definePlugin.definitions
          // 变量只能放字符串
          definitions['process.git'] = {
            commithash: JSON.stringify(gitReversionPlugin.commithash()),
            version: JSON.stringify(gitReversionPlugin.version()),
            branch: JSON.stringify(gitReversionPlugin.branch()),
          }
          definitions['process.className'] = {
            name: JSON.stringify('BK2211')
          }
          return webpackConfig;
        },
      },
      options: {}
    }
  ],
}